package locadora.dados;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import locadora.negocio.produtos.Item;
import miscelanea.Data;

import org.junit.BeforeClass;
import org.junit.Test;

public class RepositorioItemTest {
	
	static RepositorioItem repositorio;

	@BeforeClass
	public static void inicializa() throws Exception {
		repositorio = new RepositorioItem();
		Item i1 = new Item("T1", 2, new Data("01021940"), "D1", 35, 0);
		Item i2 = new Item("T2", 2, new Data("01021940"), "D1", 35, 0);
		Item i3 = new Item("T3", 2, new Data("01021940"), "D1", 35, 0);
		Item i4 = new Item("T4", 2, new Data("01021940"), "D1", 35, 0);
		repositorio.adiciona(i1);
		repositorio.adiciona(i2);
		repositorio.adiciona(i3);
		repositorio.adiciona(i4);
	}
	
//	@Test
//	public void testFinalize() throws IOException {
//		testArmazena();
//	}

	@Test
	public void testListarObjetos() {
		ArrayList<Object> lista = repositorio.ListarObjetos();
		
		assertTrue(lista.equals(repositorio.arrayItem));
	}

	@Test
	public void testArmazena() throws IOException {
		File file = new File(repositorio.NOME_ARQUIVO);
		
		file.delete();
		
		repositorio.armazena();
		
		assertTrue(file.exists());
	}

	@Test
	public void testBusca() throws Exception {
		Item i = new Item("T0", 2, new Data("01021940"), "D1", 35, 0);
		String q2 = "0";
		
		Item a1 = repositorio.busca(i.getChave());
		assertNull(a1);
		
		assertNull(repositorio.busca(q2));
	}

	@Test
	public void testAdiciona() throws Exception {
		Item i5 = new Item("T5", 2, new Data("01021940"), "D1", 35, 0);
		
		repositorio.adiciona(i5);
		
		assertNotNull(repositorio.busca(i5.getChave()));
	}

	@Test
	public void testRemove() throws Exception {
		Item i6 = new Item("T5", 2, new Data("01021940"), "D1", 35, 0);
		
		repositorio.adiciona(i6);
		
		//repositorio.remove(i6.getChave());
		
		assertNotNull(repositorio.busca(i6.getChave()));
	}

}
